home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / msql-1.0.6 / src / common / debug.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  3KB  |  189 lines

  1. /*
  2. **    debug.c    - Shared debug output routines
  3. **
  4. **
  5. ** Copyright (c) 1993  David J. Hughes
  6. **
  7. ** Permission to use, copy, and distribute for non-commercial purposes,
  8. ** is hereby granted without fee, providing that the above copyright
  9. ** notice appear in all copies and that both the copyright notice and this
  10. ** permission notice appear in supporting documentation.
  11. **
  12. ** This software is provided "as is" without any expressed or implied warranty.
  13. **
  14. */
  15.  
  16. #ifndef lint
  17. static char RCSid[] = 
  18.     "debug.c,v 1.3 1994/08/19 08:02:54 bambi Exp";
  19. #endif 
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <varargs.h>
  25. #include "debug.h"
  26.  
  27. #include <common/portability.h>
  28.  
  29. int         debugLevel=0;
  30. extern char    PROGNAME[];
  31. int        titleFlag = 0;
  32.  
  33.  
  34. void initDebug()
  35. {
  36.     char    *env,
  37.         *tmp,
  38.         *tok;
  39.  
  40.     env = getenv("MINERVA_DEBUG");
  41.     if(env)
  42.     {
  43.         tmp = (char *)strdup(env);
  44.     }
  45.     else
  46.         return;
  47.     printf("\n-------------------------------------------------------\n");
  48.     printf("MINERVA_DEBUG found.  %s started with the following :-\n\n",
  49.         PROGNAME);
  50.     tok = (char *)strtok(tmp,":");
  51.     while(tok)
  52.     {
  53.         if (strcmp(tok,"cache") == 0)
  54.         {
  55.             debugLevel |= MOD_CACHE;
  56.             printf("Debug level : cache\n");
  57.         }
  58.         if (strcmp(tok,"query") == 0)
  59.         {
  60.             debugLevel |= MOD_QUERY;
  61.             printf("Debug level : query\n");
  62.         }
  63.         if (strcmp(tok,"general") == 0)
  64.         {
  65.             debugLevel |= MOD_GENERAL;
  66.             printf("Debug level : general\n");
  67.         }
  68.         if (strcmp(tok,"error") == 0)
  69.         {
  70.             debugLevel |= MOD_ERR;
  71.             printf("Debug level : error\n");
  72.         }
  73.         if (strcmp(tok,"key") == 0)
  74.         {
  75.             debugLevel |= MOD_KEY;
  76.             printf("Debug level : key\n");
  77.         }
  78.         if (strcmp(tok,"malloc") == 0)
  79.         {
  80.             debugLevel |= MOD_MALLOC;
  81.             printf("Debug level : malloc\n");
  82.         }
  83.         if (strcmp(tok,"trace") == 0)
  84.         {
  85.             debugLevel |= MOD_TRACE;
  86.             printf("Debug level : trace\n");
  87.         }
  88.         if (strcmp(tok,"mmap") == 0)
  89.         {
  90.             debugLevel |= MOD_MMAP;
  91.             printf("Debug level : mmap\n");
  92.         }
  93.         if (strcmp(tok,"access") == 0)
  94.         {
  95.             debugLevel |= MOD_ACCESS;
  96.             printf("Debug level : access\n");
  97.         }
  98.         if (strcmp(tok,"proctitle") == 0)
  99.         {
  100.             titleFlag=1;
  101.             printf("Debug level : proctitle\n");
  102.         }
  103.         tok = (char *)strtok(NULL,":");
  104.     }
  105.     (void)free(tmp);
  106.     printf("\n-------------------------------------------------------\n\n");
  107. }
  108.  
  109.  
  110. void _msqlDebug(va_alist)
  111.     va_dcl
  112. {
  113.         va_list args;
  114.     char    msg[1024],
  115.         *fmt;
  116.     int    module,
  117.         out = 0;
  118.  
  119.     va_start(args);
  120.     module = (int) va_arg(args, int *);
  121.     if (! (module & debugLevel))
  122.     {
  123.         va_end(args);
  124.         return;
  125.     }
  126.  
  127.     fmt = (char *)va_arg(args, char *);
  128.     if (!fmt)
  129.             return;
  130.     (void)vsprintf(msg,fmt,args);
  131.     va_end(args);
  132.     printf("[%s] %s",PROGNAME,msg);
  133.     fflush(stdout);
  134. }
  135.  
  136.  
  137. int debugSet(module)
  138.     int    module;
  139. {
  140.     if (! (module & debugLevel))
  141.                 return(0);
  142.      return(1);
  143. }
  144.  
  145.  
  146.  
  147. _msqlTrace(va_alist)
  148.     va_dcl
  149. {
  150.     va_list args;
  151.     char    msg[1024],
  152.         *fmt,
  153.         *tag;
  154.     int    loop,
  155.         dir;
  156.     static    int indent = 0;
  157.     static     char inTag[] = "-->",
  158.              outTag[] = "<--";
  159.  
  160.     va_start(args);
  161.     if (! (debugLevel & MOD_TRACE))
  162.     {
  163.         va_end(args);
  164.         return;
  165.     }
  166.  
  167.     dir = va_arg(args, int);
  168.     if (dir == TRACE_IN)
  169.     {
  170.         tag = inTag;
  171.         indent++;
  172.     }
  173.     else
  174.         tag = outTag;
  175.     fmt = (char *)va_arg(args, char *);
  176.     if (!fmt)
  177.             return;
  178.     (void)vsprintf(msg,fmt,args);
  179.     va_end(args);
  180.     printf("[%s] ",PROGNAME);
  181.     for (loop = 1; loop <indent; loop++)
  182.         printf("  ");
  183.     printf("%s %s\n",tag,msg);
  184.     fflush(stdout);
  185.     if (dir == TRACE_OUT)
  186.         indent--;
  187. }
  188.  
  189.